$(document).ready(function(){
    show_items_count();
    show_cart_list();
    show_sum_money();
    updata_add_cart_list_event();
    add_button_event();
});
function show_element_to_page(element,value){
    $(element).html(value);
}
function show_items_count(){
    var count = localStorage.getItem("sum_count") || 0;
    show_element_to_page("#sum_count",count);
}
function get_cart_list(){
    return  JSON.parse(localStorage.getItem("cart_list"));
}
function set_cart_list(goods_items){
    localStorage.setItem("cart_list",JSON.stringify(goods_items));
}
function show_cart_list(){
    var goods_items=get_cart_list();
    $("#form").tmpl(goods_items).appendTo("#cart_list");
}
function add_button_event(){
    $(".button").click(function(){
        window.location.href ="/untitled1/html/payment_page.html"
    });
}
function get_change_count(mark_id){
    return  mark_id.match('sub') ? -1 : 1 ;
}
function get_goods_id(mark_id){
    return mark_id.substr(0, mark_id.length - 3);
}
function updata_add_cart_list_event(){
    $.getJSON("../javascript/save_id.json",function(info){
        $("td button").click(function (){
            var mark_id = this.id;
            var item_id = get_goods_id(mark_id);
            var change_count = get_change_count(mark_id);
            updata_goods_list(change_count, item_id,info);
            updata_current_element(item_id);
            show_sum_count(change_count);
            show_sum_money();
            is_cart_empty();
        });
    });
}
function updata_goods_list(change_count,item_id,info){
    var goods_items=get_cart_list();
    updata_goods_count(goods_items,item_id,change_count,info);
    remove_goods_item(goods_items);
    set_cart_list(goods_items);
}
function get_goods_item(goods_items,item_id){
    return _.find(goods_items,function(item){
        return item_id == item.id;
    });
}
function updata_goods_count(goods_items,item_id,change_count,info){
    var goods_item=get_goods_item(goods_items,item_id);
    goods_item.count += change_count;
    goods_item.free_count=get_save_count(item_id,goods_item,info);
    return  goods_item;
}
function get_save_count(item_id,goods_item,info){
    var free_count=0;
    if(info.indexOf(item_id)!=-1){
        free_count= Math.floor(goods_item.count/3);
    }
    return free_count;
}
function remove_goods_item(goods_items){
    _.remove(goods_items,function(info){
        return  info.count== 0;
    });
}
function updata_current_element(item_id){
    var goods_items=get_cart_list();
    var goods_item=get_goods_item(goods_items,item_id);
    if(goods_item ==undefined){
        remove_current_row_element(item_id);
    }else {
        show_goods_item_count(goods_item, item_id);
        show_goods_subtotal(goods_item, item_id);
    }
}
function remove_current_row_element(item_id){
    $("#"+item_id+"input").parent().parent("tr").remove();
}
function show_goods_item_count(goods_item,item_id){
    $("#"+item_id+"input").val(goods_item.count);
}
function show_goods_subtotal(goods_item,item_id){
    var subtotal=get_current_item_subtotal(goods_item);
    show_element_to_page("#" + item_id +"subtotal",subtotal);
}
function get_current_item_subtotal(goods_item){
    var subtotal = goods_item.count * goods_item.price+"元";
    if (goods_item.free_count != 0){
        subtotal=(goods_item.count - goods_item.free_count) * goods_item.price + "元" + "(原价:  " + (goods_item.count) * (goods_item.price) + "元)";
    }
    return subtotal;
}
function show_sum_money(){
    var sum_money=get_sum_money();
    show_element_to_page("#sum_money",sum_money);
}
function get_sum_money(){
    var sum_money=0;
    var goods_items=get_cart_list();
    _.each(goods_items,function(item){
        sum_money += (item.count)*(item.price);
    });
    sum_money=sum_money.toFixed(2)+"元";
    return sum_money;
}
function show_sum_count(change_count){
    var count=$("#sum_count").html();
    count　= parseFloat(count)+ change_count;
    localStorage.setItem("sum_count",count);
    show_element_to_page("#sum_count",count);
}
function is_cart_empty(){
    var goods_items=get_cart_list();
    if(goods_items.length==0){
        window.location.href="/untitled1/html/item_list.html"
    }
}